package com.mer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mer.entity.Course;
import com.mer.entity.CourseReview;
import com.mer.entity.Subject;
import com.mer.mapper.CourseReviewMapper;
import com.mer.mapper.SubjectMapper;
import com.mer.service.CourseReviewService;
import com.mer.vo.CommonResult;
import com.mer.vo.CourseReviewVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: IIPhase-project
 * @description: CourseReviewService实现类
 * @author: 陈铠
 * @create: 2021-10-21 14:52
 **/
@Service
public class CourseReviewServiceImpl extends ServiceImpl<CourseReviewMapper, CourseReview> implements CourseReviewService {
    @Resource
    private CourseReviewMapper courseReviewMapper;
    @Resource
    private SubjectMapper subjectMapper;

    @Override
    public CommonResult selectTobeReviewedCourse(Integer page, Integer limit, CourseReviewVo courseReviewVo) {
        Page<CourseReviewVo> courseReviewVoPage = new Page<>(page, limit);
        QueryWrapper<CourseReviewVo> courseReviewVoQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(courseReviewVo.getTitle())) {
            courseReviewVoQueryWrapper.like("title", courseReviewVo.getTitle());
        }
        if (StringUtils.isNotEmpty(courseReviewVo.getStatus())) {
            courseReviewVoQueryWrapper.like("status", courseReviewVo.getStatus());
        }
        Page<CourseReviewVo> courseReviewVoPage1 = courseReviewMapper.selectTobeReviewedCourse(courseReviewVoPage, courseReviewVoQueryWrapper);

        List<CourseReviewVo> records = courseReviewVoPage1.getRecords();
        List<Subject> subjects = subjectMapper.selectList(null);
        for (CourseReviewVo courseReviewVo1 : records) {
            String ex = "";
            String nx = selectSubjectName(subjects, courseReviewVo1.getSubjectId());
            String[] split = nx.split(">");
            for (int i = split.length - 1; i >= 0; i--) {
                if (i != 0) {
                    ex += split[i] + ">";
                } else {
                    ex += split[i];
                }
            }
            courseReviewVo1.setSubjectName(ex);
        }
        return new CommonResult(200, "查询成功", courseReviewVoPage1);
    }

    /**
     * 课程分类列的递归方法，selectCourseByPage需要调用
     * @param subjects
     * @param id
     * @return
     */
    public static String selectSubjectName(List<Subject> subjects, String id) {
        String name = "";
        for (Subject subject : subjects) {
            if (id.equals(subject.getId())) {
                //拼接各个分级的课程
                name += "【" + subject.getTitle() + "】" + ">";
                if (!subject.getParentId().equals("0")) {
                    String childName = selectSubjectName(subjects, subject.getParentId());
                    name += childName;
                }
            }
        }
        return name;
    }

    @Override
    public CommonResult modifyCourseCheck(CourseReviewVo courseReviewVo) {
        int i = courseReviewMapper.modifyCourseCheck(courseReviewVo);
        if (i > 0) {
            return new CommonResult(200, "审核通过", "yes");
        } else {
            return new CommonResult(500, "审核驳回", "no");
        }
    }
}
